<template>
  <DialogSubmitForm v-bind="formFrame" ref="form" />
</template>
<script setup lang="ts">
import { MDialogSubmitForm as DialogSubmitForm } from "page-com";
import { shallowReactive, useTemplateRef } from "vue";
import { addWarehouse } from "@/api/warehouse/warehouseApi";
import { DialogSubmitFormFrame, DialogSubmitFormInstance } from "page-com";
import { successMsg } from "@/utils/message";
import { getRegionTree } from "@/api/warehouse/regionApi";
const form = useTemplateRef<DialogSubmitFormInstance>("form");
const formFrame = shallowReactive<DialogSubmitFormFrame>({
  submitFun: async function (data) {
    data.regionId = data.regionId.join(",");
    let res = await addWarehouse(data);
    if (res.code == RESPONSE_CODE.SUCCESS) {
      successMsg("添加成功");
      emit("saveSuccess", data);
      form.value.hide();
    }
  },
  width: "400px",
  items: [
    {
      frameType: "input",
      key: "name",
      label: "仓库名称",
      validations: {
        required: true
      }
    },
    {
      frameType: "cascader",
      key: "regionId",
      label: "省/市/区",
      cascaderOptions: getRegionTree().then(v => v.data),
      validations: {
        required: true
      }
    },
    {
      frameType: "input",
      key: "address",
      label: "详细地址",
      validations: {
        required: true
      }
    }
  ]
});
function show() {
  form.value.show();
}

defineExpose({ show });
const emit = defineEmits<{
  saveSuccess: [data: ShopWarehouseVo];
}>();
</script>
